//
//  BannerListVC.swift
//  HelloSwift
//
//  Created by alankong on 2016/11/18.
//  Copyright © 2016年 tuotiansudai. All rights reserved.
//

import UIKit

class BannerListVC: UITableViewController {
    
    var bannerResult: BannerResult = BannerResult()
    var arrBanners: NSArray = NSArray()
    var reuseIdentifier = "BannerListIdentifier"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Banner列表"
        self.tableView .register(BannerListCell.classForCoder(), forCellReuseIdentifier: self.reuseIdentifier)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getBaner()
    }
    
    func getBaner() {
        let opManager = AFHTTPRequestOperationManager();
        opManager.requestSerializer = AFJSONRequestSerializer();
        opManager.requestSerializer.timeoutInterval = 30;
        let urlString: String = "https://api.tuotiansudai.com/v1.0/get/banner";
        let param: NSDictionary = ["baseParam":["platform":"iOS",
                                                "channel":"AppStore",
                                                "appVersion":"3.3",
                                                "osVersion":"10.0",
                                                "deviceId":"123",
                                                "deviceModel":"iPhone6",
                                                "screenW":"750",
                                                "screenH":"1334"]];
        
        opManager.post(urlString, parameters: param, success: { (operation, object) in
            let dicResult: NSDictionary = object as! NSDictionary;
            if (Int(dicResult.object(forKey: "code") as! String) == 0) {
                let dicData: NSDictionary = dicResult.object(forKey: "data") as! NSDictionary;
                self.bannerResult = JSONDeserializer<BannerResult>.deserializeFrom(dict: dicData)!
                self.tableView.reloadData()
            } else {
                NSLog(dicResult.object(forKey: "message") as! String)
            }
            
            }, failure: { (operation, error) in
            NSLog("get banner failed")
        })
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.bannerResult.pictures == nil) {
            return 0
        }
        return (self.bannerResult.pictures!.count)*3
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140.0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:BannerListCell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: indexPath) as! BannerListCell
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        
        let bannerVO:BannerVO = (self.bannerResult.pictures![indexPath.row % self.bannerResult.pictures!.count])
        cell.setBannerVO(bannerVO: bannerVO)
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let VC:BannerDetailVC = BannerDetailVC();
        let bannerVO:BannerVO = self.bannerResult.pictures![indexPath.row % self.bannerResult.pictures!.count];
        VC.url = bannerVO.url!
        self.navigationController?.pushViewController(VC, animated: true)
        tableView.deselectRow(at: indexPath, animated: true)
        
        //Model转JSON
//        let string: String = JSONSerializer.serializeToJSON(object: self.bannerResult)!
//        let dicString:NSDictionary = string.objectFromJSONString() as! NSDictionary
//        let string2 = dicString.jsonString()
//        let banner: BannerResult = JSONDeserializer<BannerResult>.deserializeFrom(json: string)!
//        let bannerVO2:BannerVO = banner.pictures![indexPath.row % self.bannerResult.pictures!.count];
    }
}
